﻿using DetectScars.model;
using Newtonsoft.Json.Linq;
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using static SkiaSharp.HarfBuzz.SKShaper;

namespace DetectScars.Menus
{
    public partial class ProbeForm : Form
    {
        Probe? probe = null;
        public ProbeForm()
        {
            InitializeComponent();
            //numericUpDown3支持小数
            numericUpDown3.DecimalPlaces = 2;
            numericUpDown2.DecimalPlaces = 2;
            numericUpDown5.DecimalPlaces = 2;
        }

        internal ProbeForm(Probe probe)
        {
            InitializeComponent();
            //numericUpDown3支持小数
            numericUpDown3.DecimalPlaces = 2;
            numericUpDown2.DecimalPlaces = 2;
            numericUpDown5.DecimalPlaces = 2;
            this.probe = probe;
            textBox1.Text = probe.Name;
            numericUpDown5.Value = (decimal)probe.Description;
            numericUpDown1.Value = probe.ChipCount;
            numericUpDown2.Value = (decimal)probe.ChipIndex;
            numericUpDown3.Value = (decimal)probe.ChipWidth;
            richTextBox1.Text = probe.ChipXPox;
            richTextBox2.Text = probe.ChipYPox;
            richTextBox3.Text = probe.ChipZPox;
            button1.Text = "修改";
        }

        internal Probe? AddProbe()
        {
            return probe;
        }
        private void button1_Click(object sender, EventArgs e)
        {
            //校验数据
            string probeName = textBox1.Text;
            if (string.IsNullOrEmpty(probeName))
            {
                MessageBox.Show("请输入探头编号", "提示", MessageBoxButtons.OK, MessageBoxIcon.Warning);
                return;
            }
            float Description = (float)numericUpDown5.Value;
            if (Description == 0)
            {
                MessageBox.Show("频率不能为0", "提示", MessageBoxButtons.OK, MessageBoxIcon.Warning);
                return;
            }
            int ChipCount = (int)numericUpDown1.Value;
            if (ChipCount == 0)
            {
                MessageBox.Show("晶片数量不能为0", "提示", MessageBoxButtons.OK, MessageBoxIcon.Warning);
                return;
            }
            float ChipIndex = (float)numericUpDown2.Value;
            if (ChipIndex == 0)
            {
                MessageBox.Show("晶片间距不能为0", "提示", MessageBoxButtons.OK, MessageBoxIcon.Warning);
                return;
            }
            float ChipWidth = (float)numericUpDown3.Value;
            if (ChipWidth == 0)
            {
                MessageBox.Show("晶片宽度不能为0", "提示", MessageBoxButtons.OK, MessageBoxIcon.Warning);
                return;
            }
            string ChipXPox = "";
            string ChipYPox = "";
            string ChipZPox = "";
            //ChipZPox有ChipCount个0且空格分开
            for (int i = 0; i < ChipCount; i++)
            {
                ChipZPox += "0 ";
            }
            ChipZPox = ChipZPox.Trim();
            //ChipYPox有ChipCount个0且空格分开
            for (int i = 0; i < ChipCount; i++)
            {
                ChipYPox += "0 ";
            }
            ChipYPox = ChipYPox.Trim();
            //ChipXPox
            for (int i = 0; i < ChipCount; i++)
            {
                ChipXPox += ChipIndex * i + " ";
            }
            ChipXPox = ChipXPox.Trim();
            if (probe == null)
            {
                string uuid = Guid.NewGuid().ToString();
                probe = new Probe() { Id = uuid,Name= probeName };
            }
            probe.Name = probeName;
            probe.Description = Description;
            probe.ChipCount = ChipCount;
            probe.ChipIndex = ChipIndex;
            probe.ChipWidth = ChipWidth;
            probe.ChipXPox = ChipXPox;
            probe.ChipYPox = ChipYPox;
            probe.ChipZPox = ChipZPox;
            probe.IsDeleted = false;
            //存入json文件
            DialogResult = DialogResult.OK;

        }

        private void button2_Click(object sender, EventArgs e)
        {
            DialogResult = DialogResult.Cancel;
        }

        private void numericUpDown3_Validating(object sender, CancelEventArgs e)
        {
            NumericUpDown numericUpDown = (NumericUpDown)sender;
            //校验输入数据为数且为大于0
            float result = 0;
            if (!float.TryParse(numericUpDown.Value.ToString(), out result) || result <= 0)
            {
                e.Cancel = true;
            }
        }

        private void numericUpDown2_Validating(object sender, CancelEventArgs e)
        {
            NumericUpDown numericUpDown = (NumericUpDown)sender;
            //校验输入数据为数且为大于0
            if (!float.TryParse(numericUpDown.Value.ToString(), out float result) || result <= 0)
            {
                e.Cancel = true;
            }
        }

        private void numericUpDown1_Validating(object sender, CancelEventArgs e)
        {
            NumericUpDown numericUpDown = (NumericUpDown)sender;
            //校验输入数据为数且为大于0
            if (!float.TryParse(numericUpDown.Value.ToString(), out float result) || result <= 0)
            {
                e.Cancel = true;
            }
        }

        private void numericUpDown5_Validating(object sender, CancelEventArgs e)
        {
            NumericUpDown numericUpDown = (NumericUpDown)sender;
            //校验输入数据为数且为大于0
            if (!float.TryParse(numericUpDown.Value.ToString(), out float result) || result <= 0)
            {
                e.Cancel = true;
            }
        }

        private void numericUpDown1_ValueChanged(object sender, EventArgs e)
        {
            if (!float.TryParse(numericUpDown1.Value.ToString(), out float result) || result <= 0)
            {
                return;
            }
            string ChipXPox = "";
            string ChipYPox = "";
            string ChipZPox = "";
            int ChipCount = (int)numericUpDown1.Value;
            //ChipZPox有ChipCount个0且空格分开
            for (int i = 0; i < ChipCount; i++)
            {
                ChipZPox += "0 ";
            }
            ChipZPox = ChipZPox.Trim();
            richTextBox3.Text = ChipZPox;
            //ChipYPox有ChipCount个0且空格分开
            for (int i = 0; i < ChipCount; i++)
            {
                ChipYPox += "0 ";
            }
            ChipYPox = ChipYPox.Trim();
            richTextBox2.Text = ChipYPox;
            float ChipIndex = (float)numericUpDown2.Value;
            if (!float.TryParse(numericUpDown2.Value.ToString(), out float s) || s <= 0)
            {
                return;
            }
            //ChipXPox
            for (int i = 0; i < ChipCount; i++)
            {
                ChipXPox += ChipIndex * i + " ";
            }
            ChipXPox = ChipXPox.Trim();
            richTextBox1.Text = ChipXPox;
        }


        private void numericUpDown2_ValueChanged(object sender, EventArgs e)
        {
            string ChipXPox = "";
            float ChipIndex = (float)numericUpDown2.Value;
            int ChipCount = (int)numericUpDown1.Value;
            if (!float.TryParse(numericUpDown2.Value.ToString(), out float s) || s <= 0)
            {
                return;
            }
            //ChipXPox
            for (int i = 0; i < ChipCount; i++)
            {
                ChipXPox += ChipIndex * i + " ";
            }
            ChipXPox = ChipXPox.Trim();
            richTextBox1.Text = ChipXPox;
        }
    }
}
